import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {LoginAutherService} from './login-auther.service';
import {UserService} from '../services/user/user.service';

@Injectable()
export class LoginAutherGuard implements CanActivate {

  constructor(
    private loginAuther: LoginAutherService,
    private router: Router,
    private userService: UserService) {

  }

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
    if (!this.loginAuther.isLogin) {
      this.userService.message = '请先登录在进行操作！';
      this.router.navigateByUrl('/login');
      return false;
    }
    // todo: /.
    return true;
  }
}
